package com.jihox.abookpro;


import java.util.ArrayList;
import java.util.List;

import com.jihox.abookpro.R;
import com.jihox.abookpro.adapter.ViewPagerAdapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


/**
 * 
 * @{# GuideActivity.java Create on 2013-5-2 下午10:59:08
 * 
 *     class desc: 引导界面
 * 
 *     <p>
 *     Copyright: Copyright(c) 2013
 *     </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a>
 * 
 * 
 */
public class GuideActivity extends BaseActivity implements OnPageChangeListener {

	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;
	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	// 底部小点图片
	private ImageView[] dots;

	// 记录当前选中位置
	private int currentIndex;
	private boolean isFromHelp = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide);
		isFromHelp =  this.getIntent().getBooleanExtra("fromHelp", false);
		// 初始化页面
		initViews();

		// 初始化底部小点
		initDots();
	}

	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList<View>();
		View view_one = inflater.inflate(R.layout.what_new_one, null);
		View view_two = inflater.inflate(R.layout.what_new_two, null);
		View view_three = inflater.inflate(R.layout.what_new_three, null);
		View view_four = inflater.inflate(R.layout.what_new_four, null);
		
		ImageView image1 = (ImageView)view_one.findViewById(R.id.image_in_guide_page);
		ImageView image2 = (ImageView)view_two.findViewById(R.id.image_in_guide_page);
		ImageView image3 = (ImageView)view_three.findViewById(R.id.image_in_guide_page);
		ImageView image4 = (ImageView)view_four.findViewById(R.id.image_in_guide_page);
		
		// 初始化引导图片列表
		views.add(view_one);
		views.add(view_two);
		views.add(view_three);
		views.add(view_four);
		views.add(inflater.inflate(R.layout.activity_homepage, null));
		
		if (isFromHelp){
			image1.setBackgroundResource(R.drawable.help_1);
			image2.setBackgroundResource(R.drawable.help_2);
			image3.setBackgroundResource(R.drawable.help_3);
			image4.setBackgroundResource(R.drawable.help_4);
		}
		// 初始化Adapter
		vpAdapter = new ViewPagerAdapter(views, this);

		vp = (ViewPager) findViewById(R.id.viewpager);
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
	}

	private void initDots() {
		LinearLayout ll = (LinearLayout) findViewById(R.id.dots);

		dots = new ImageView[views.size()-1];

		// 循环取得小点图片
		for (int i = 0; i < views.size()-1; i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}

	private void setCurrentDot(int position) {
		if (position < 0 || position > views.size() - 2
				|| currentIndex == position) {
			return;
		}

		dots[position].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = position;
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	private void goLogin() {
		// 跳转
		finish();
//		Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
		Intent intent = new Intent(GuideActivity.this,HomeChangeActivity.class);
		//intent.putExtra("ComeFrom", "Start");
		//intent.putExtra("FragmentIndex", 0);
		startActivity(intent);
	}

	@Override
	protected void onDestroy() {
		for(View v : views){
			ImageView iv = (ImageView) v.findViewById(R.id.image_in_guide_page);
			if(iv!=null){
				BitmapDrawable bd = (BitmapDrawable)iv.getBackground();
				if(bd!=null&&bd.getBitmap()!=null&&!bd.getBitmap().isRecycled()){
					iv.setBackgroundResource(0);
					bd.setCallback(null);
					bd.getBitmap().recycle();
					System.gc();
				}
			}
		}
		super.onDestroy();
	}

	/**
	 * 
	 * method desc：设置已经引导过了，下次启动不用再次引导
	 */
	private void setGuided() {
		SharedPreferences preferences = getSharedPreferences(
				SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		// 存入数据
		editor.putInt("versionCode", this.getIntent().getIntExtra("curVersionCode", 0));
		// 提交修改
		editor.commit();
//		MyApplication.getInstance().getCustomerCodeFromServer(null);
	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
		if (arg0 == views.size() - 1){
			setGuided();
			if (isFromHelp){
				finish();
			}else{
				goLogin();
			}
			
		}
	}

}

